package algs4.sort;

public class Selection
{
    /* Static Methods */
    public static void sort(Comparable[] a) {
        int n = a.length;

        for (int i = 0; i < n - 1; i++) {
            int min = i;

            for (int j = i + 1; j < n; j++) {
                if (a[min].compareTo(a[j]) > 0)
                    min = j;
            }

            // swap a[i] and a[min]
            Comparable v = a[i];

            a[i]   = a[min];
            a[min] = v;
        }
    }

    public static void sort(Comparable[] a, int st, int ed) {
        for (int i = st; i < ed; i++) {
            int min = i;

            for (int j = i + 1; j <= ed; j++) {
                if (a[min].compareTo(a[j]) > 0)
                    min = j;
            }

            // swap a[i] and a[min]
            Comparable v = a[i];

            a[i]   = a[min];
            a[min] = v;
        }
    }
}
